package com.xinchao.ck_mips.view;

import android.app.Dialog;
import android.app.smdt.SmdtManager;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.xinchao.ck_mips.R;
import com.xinchao.ck_mips.base.MyBasePresenter;
import com.xinchao.ck_mips.broadcast.NetWorkChangeReceiver;
import com.xinchao.rxtools.rxhttp.tools.IDisposable;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by wq on 2019/4/26.
 */
public abstract class BaseMvpActivity<V, T extends MyBasePresenter<V>> extends AppCompatActivity implements IDisposable {
    protected T mPresenter;
    public Dialog loadingDialog;
    public CompositeDisposable compositeDisposable = new CompositeDisposable();
    protected NetWorkChangeReceiver mNetWorkChangeReceiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = createPrestener();
        registerNetWorkChangeReceiver();
        mPresenter.attachView((V) this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Build.MODEL.contains("3280") || Build.MODEL.contains("IoT3288") || Build.MODEL.contains("DS835")) {
            //隐藏状态栏
            SmdtManager smt = SmdtManager.create(this);
            smt.smdtSetStatusBar(this, false);
        }

        /*if (Build.MODEL.contains("DS835")) {
            hideNavigation();
            getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    hideNavigation();
                }
            });
        }*/
    }

    protected void hideNavigation() {
        //隐藏虚拟键
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
            getWindow().getDecorView().setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            int options = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            getWindow().getDecorView().setSystemUiVisibility(options);
        }
    }

    protected abstract T createPrestener();

    /**
     * 显示加载框
     */
    protected void showUILoading() {
        if (loadingDialog == null) {
            View dialogView = getLayoutInflater().inflate(R.layout.loading_dialog, null);
            loadingDialog = new Dialog(this, R.style.loading_dialog);
            loadingDialog.setCanceledOnTouchOutside(false);
            loadingDialog.setContentView(dialogView);
        }
        if (!loadingDialog.isShowing())
            loadingDialog.show();
    }

    /**
     * 注册网络变化广播
     */
    protected void registerNetWorkChangeReceiver() {
        mNetWorkChangeReceiver = new NetWorkChangeReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mNetWorkChangeReceiver, intentFilter);
    }

    /**
     * 隐藏加载框
     */
    protected void hideUILoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
       /* removeAllDisposable();
        compositeDisposable = null;
        mPresenter.detachView();*/
        if (mNetWorkChangeReceiver != null) {
            unregisterReceiver(mNetWorkChangeReceiver);
        }

    }

    @Override
    public void addDisposable(Disposable disposable) {
        if (disposable == null || compositeDisposable == null) {
            return;
        }
        compositeDisposable.add(disposable);
    }

    @Override
    public void removeDisposable(Disposable disposable) {
        if (disposable == null || compositeDisposable == null) {
            return;
        }
        compositeDisposable.remove(disposable);
    }

    @Override
    public void removeAllDisposable() {
        compositeDisposable.clear();
    }
}
